<script lang="ts" setup>
import DataEase from '@/assets/svg/DataEase.svg'
import { ref, reactive, onMounted, computed, nextTick } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { FormRules, FormInstance } from 'element-plus-secondary'
import { Icon } from '@/components/icon-custom'
import { autologinApi, loginApi, queryDekey } from '@/api/login'
import { useCache } from '@/hooks/web/useCache'
import { useAppStoreWithOut } from '@/store/modules/app'
import { CustomPassword } from '@/components/custom-password'
import { useUserStoreWithOut } from '@/store/modules/user'
import { useAppearanceStoreWithOut } from '@/store/modules/appearance'
import { rsaEncryp } from '@/utils/encryption'
import router from '@/router'
import { ElMessage } from 'element-plus-secondary'
import { XpackComponent } from '@/components/plugin'
import { logoutHandler } from '@/utils/logout'
import DeImage from '@/assets/login-desc-de.png'
import elementResizeDetectorMaker from 'element-resize-detector'
import { cleanPlatformFlag } from '@/utils/utils'
import xss from 'xss'
const { wsCache } = useCache()
const appStore = useAppStoreWithOut()
const userStore = useUserStoreWithOut()
const appearanceStore = useAppearanceStoreWithOut()
const { t } = useI18n()
const getCurLocation = () => {
  let queryRedirectPath = '/preview?dvId='
  if (router.currentRoute.value.query.redirect) {
    queryRedirectPath = router.currentRoute.value.query.redirect as string
    console.log('queryRedirectPath', queryRedirectPath)
  }
  return queryRedirectPath
}

onMounted(async () => {
  //'admin'
  const name = getParam()['dtuser']
  const pwd = '123456'
  //debugger
  if (!wsCache.get(appStore.getDekey)) {
    const res = await queryDekey()
    wsCache.set(appStore.getDekey, res.data)
  }
  const param = { name: rsaEncryp(name), pwd: rsaEncryp(pwd) }
  autologinApi(param)
    .then(res => {
      const { token, exp, mfa } = res.data
      debugger
      userStore.setToken(token)
      userStore.setExp(exp)
      userStore.setTime(Date.now())
      userStore.setUid('')
      userStore.setName('')
      userStore.setUser()
      const queryRedirectPath = getCurLocation()
      router.push({ path: queryRedirectPath })
    })
    .catch(() => {
      duringLogin.value = false
    })
})
const getParam = () => {
  // const regex = new RegExp(`[?&]${name}=([^&#]*)`)
  // const result = regex.exec(window.location.search)
  // return result ? decodeURIComponent(result[1]) : null
  // 去掉开头的 #
  const hash = new URL(window.location.href).hash.substring(1)
  const params = {}
  if (!hash.includes('?')) return params
  const queryString = hash.split('?')[1]
  queryString.split('&').forEach(pair => {
    const [key, value] = pair.split('=')
    params[key] = decodeURIComponent(value || '')
  })

  return params
}
</script>

<template>
  <div class="login-container">loading...</div>
</template>

<style lang="less" scoped>
.login-container {
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
  display: none;
}
</style>
